﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HtmlContent.Models;
using Core;
using System.Reflection;

namespace HtmlContent.Controllers
{
    [Permission("Can Create/Delete Domain, Can Modify Domain")]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            Guid BUId = (Guid)Session["BusinessUnitId"];
            ViewBag.BusinessUnitId = BUId;
            return View();
        }

        //
        // POST: /PageList/

        [HttpPost]
        public JsonResult PageList(FormCollection collection)
        {
            HtmlContentEntities hc = new HtmlContentEntities();
            Guid BUId = new Guid(collection.GetValues("BusinessUnitId")[0].ToString());
            int SkipRecs = int.Parse(collection.GetValues("iDisplayStart")[0].ToString());
            int TakeCount = int.Parse(collection.GetValues("iDisplayLength")[0].ToString());
            string strSearch = collection.GetValues("sSearch")[0].ToString();
            DataTableResponse r = new DataTableResponse();
            // Needs update when paging & sorting is implemented
            List<HtmlPage> pagelist = hc.HtmlPages.Where(m => (m.BusinessUnitId == BUId && m.PageName.Contains(strSearch) && m.IsDroppedYN == "N")).OrderBy(m => m.PageName).Skip(SkipRecs).Take(TakeCount).ToList();
            r.sEcho = collection.GetValues("sEcho")[0].ToString();
            // This query is required because the earlier query returns results for only 1 page.
            r.iTotalRecords = hc.HtmlPages.Where(m => (m.BusinessUnitId == BUId && m.PageName.Contains(strSearch) && m.IsDroppedYN == "N")).Count();
            r.iTotalDisplayRecords = r.iTotalRecords;
            foreach (HtmlPage page in pagelist)
            {
                List<string> r1 = new List<string>();
                string editurl = String.Format("<a href=\"/{0}/{1}/{2}\" id=\"editlink\" title=\"{1}\" >Edit</a>", "HtmlContent/Admin", "Edit", page.HtmlPageId.ToString());
                string detailsurl = String.Format("<a href=\"/{0}/{1}/{2}\" id=\"detailslink\" title=\"{1}\" >Details</a>", "HtmlContent/Admin", "Details", page.HtmlPageId.ToString());
                string deleteurl = String.Format("<a href=\"/{0}/{1}/{2}\" id=\"deletelink\" title=\"{1}\" >Delete</a>", "HtmlContent/Admin", "Delete", page.HtmlPageId.ToString());
                r1.Add(editurl + detailsurl + deleteurl);
                r1.Add(page.PageName);
                r1.Add(String.Format("{0:g}", page.ModifiedDT));
                r1.Add(page.ModifiedByName);
/*                r1.Add(page.CreatedByName); */
                r.aaData.Add(r1);
            }
            return Json(r);
        }

        //
        // GET: /Admin/

        public ActionResult Create()
        {
            TempData["ReturnURL"] = Request.UrlReferrer.AbsolutePath.ToString();
            HtmlContentEntities hc = new HtmlContentEntities();
            HtmlContent.Models.Page p = new HtmlContent.Models.Page();
            /*
             */
            p.PageId = Guid.NewGuid();
            p.BusinessUnitId = (Guid)Session["BusinessUnitId"];
            p.ContainerId = Guid.Empty;
            p.HtmlPageId = Guid.Empty;
            p.HtmlContentId = Guid.Empty;
            p.OnMainMenuYN = "N";
            p.PermissionId = hc.Permissions.First(m => m.Name == "Can Use Anonymously").PermissionId;

            List<Permission> plist = hc.Permissions.ToList();
            ViewBag.PermissionList = plist;
            return View(p);
        }

        //
        // POST: /Admin/
        [HttpPost]
        public ActionResult Create(Models.Page p)
        {
            if(ModelState.IsValid)
            {
                try
                {
                    HtmlContentEntities hc = new HtmlContentEntities();
                    Guid PluginId = new Guid(((System.Runtime.InteropServices.GuidAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false)[0])).Value);
                    Guid ContainerId = GetContainerId(hc, PluginId);

                    // Create Content Entity
                    HtmlContent.Models.HtmlContent c = new Models.HtmlContent();
                    c.ContentId = Guid.NewGuid();
                    c.DomainId = (Guid)Session["DomainId"];
                    c.ContentText = p.ContentText;
                    c.IsDroppedYN = "N";
                    c.CreateDT = DateTime.Now;
                    c.CreatedById = (Guid)Session["UserId"];
                    c.CreatedByName = Session["UserFullName"].ToString();
                    c.ModifiedDT = DateTime.Now;
                    c.ModifiedById = (Guid)Session["UserId"];
                    c.ModifiedByName = Session["UserFullName"].ToString();

                    // Create Template Overide
                    TemplateOveride to = new TemplateOveride();
                    to.TemplateOverideId = Guid.NewGuid();
                    to.PageId = p.PageId;
                    to.TemplateContentId = PluginId;
                    to.OverideContentId = c.ContentId;
                    to.IsDroppedYN = "N";
                    to.ModifiedDT = DateTime.Now;
                    p.TemplateOverides.Add(to);

                    // Update Page Entity
                    p.ContainerId = ContainerId;
                    p.IsTemplateYN = "N";
                    p.IsDroppedYN = "N";
                    p.CreateDT = DateTime.Now;
                    p.CreatedById = (Guid)Session["UserId"];
                    p.CreatedByName = Session["UserFullName"].ToString();
                    p.ModifiedDT = DateTime.Now;
                    p.ModifiedById = (Guid)Session["UserId"];
                    p.ModifiedByName = Session["UserFullName"].ToString();

                    // Create the Html Page Entity
                    HtmlPage hp = new HtmlPage();
                    hp.HtmlPageId = Guid.NewGuid();
                    hp.PageId = p.PageId;
                    hp.PageName = p.Title;
                    hp.BusinessUnitId = (Guid)Session["BusinessUnitId"];
                    hp.IsDroppedYN = "N";                    
                    hp.CreateDT = DateTime.Now;
                    hp.CreatedById = (Guid)Session["UserId"];
                    hp.CreatedByName = Session["UserFullName"].ToString();
                    hp.ModifiedDT = DateTime.Now;
                    hp.ModifiedById = (Guid)Session["UserId"];
                    hp.ModifiedByName = Session["UserFullName"].ToString();

                    // Add entities to Model and Save Changes
                    hc.AddToHtmlContents(c);
                    hc.AddToHtmlPages(hp);
                    hc.AddToPages(p);
                    hc.SaveChanges();


                    // Create Mainmenu entry if Specified
                    if (p.OnMainMenuYN == "Y")
                    {
                        Core.Models.MainMenu menu = new Core.Models.MainMenu();
                        menu.MenuItemId = Guid.NewGuid();
                        menu.DomainId = (Guid)Session["DomainId"];
                        menu.Name = p.Title;
                        menu.URL = p.PageName.Substring(1);
                        menu.UserPermission = hc.Permissions.First(m => m.PermissionId == p.PermissionId).Name;
                        menu.Sequence = 1;
                        menu.HideOnSmallDeviceYN = "N";
                        menu.IsDroppedYN = "N";
                        menu.CreateDT = DateTime.Now;
                        menu.CreatedById = (Guid)Session["UserId"];
                        menu.CreatedByName = Session["UserFullName"].ToString();
                        menu.ModifiedDT = DateTime.Now;
                        menu.ModifiedById = (Guid)Session["UserId"];
                        menu.ModifiedByName = Session["UserFullName"].ToString();
                        Core.Models.CoreEntities core = new Core.Models.CoreEntities();
                        core.AddToMainMenus(menu);
                        core.SaveChanges();
                    }

                    // Index the content
                    Core.Search.SearchDoc doc = new Core.Search.SearchDoc();
                    doc.DocId = p.PageId;
                    doc.BusinessUnitId = Guid.Empty; // Search convention is Guid.EWmpty if Domain wide access
                    doc.PluginId = PluginId;
                    doc.URL = p.PageName.Substring(1);
                    doc.Title = p.Title;
                    doc.Keywords = p.MetaKeywords;
                    doc.Description = p.MetaDescription;
                    doc.Content = Core.Tools.GetTextFromHtml(p.ContentText);
                    Core.Search.SearchEngine se = new Core.Search.SearchEngine((Guid)Session["DomainId"]);
                    se.IndexContent(doc);

                    try
                    {
                        if (!String.IsNullOrEmpty(TempData["ReturnURL"].ToString()))
                            if(!TempData["ReturnURL"].ToString().Contains("Admin/Index"))
                               return Redirect(p.PageName.Substring(1));
                    }
                    catch
                    {
                    }
                    return RedirectToAction("Index");
                }
                catch (Exception e)
                {

                    ModelState.AddModelError("", Tools.ExceptionMessage(e));
                }
            }
            return View(p);
        }

        private Guid GetContainerId(HtmlContentEntities hc, Guid PluginId)
        {
            Guid ContainerId;
            List<Container> clist = hc.Containers.Where(m => (m.ContainerName == "HTML Template Container" && m.PluginId == PluginId && m.IsDroppedYN == "N")).ToList();
            if (clist.Count <= 0)
            {
                // Create Container Entity
                ContainerId = Guid.NewGuid();
                Container container = new Container();
                container.ContainerId = ContainerId;
                container.ContainerType = "C";
                container.ShowChromeYN = "N";
                container.ContainerName = "HTML Template Container";
                container.Title = "Simple HTML Page";
                container.Width = 0;
                container.ParentContainerId = null;
                container.PluginId = PluginId;
                container.ContentId = PluginId;
                container.IsDroppedYN = "N";
                container.CreateDT = DateTime.Now;
                container.CreatedById = (Guid)Session["UserId"];
                container.CreatedByName = Session["UserFullName"].ToString();
                container.ModifiedDT = DateTime.Now;
                container.ModifiedById = (Guid)Session["UserId"];
                container.ModifiedByName = Session["UserFullName"].ToString();
                hc.AddToContainers(container);
                hc.SaveChanges();
            }
            else
            {
                ContainerId = clist[0].ContainerId;
            }
            return ContainerId;
        }

        //
        // GET: /HtmlContent/PageEdit

        public ActionResult PageEdit(Guid id)
        {
            HtmlContentEntities hc = new HtmlContentEntities();

            Models.HtmlPage hp = hc.HtmlPages.First(m => m.PageId == id);
            return RedirectToAction("Edit", new { id=hp.HtmlPageId});
        }

        //
        // GET: /HtmlContent/Edit

        public ActionResult Edit(Guid id)
        {
            TempData["ReturnURL"] = Request.UrlReferrer.AbsolutePath.ToString();
            HtmlContentEntities hc = new HtmlContentEntities();
            Guid PluginId = new Guid(((System.Runtime.InteropServices.GuidAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false)[0])).Value);

            Models.HtmlPage hp = hc.HtmlPages.First(m => m.HtmlPageId == id);
            Models.Page page = hc.Pages.First(m => m.PageId == hp.PageId);
            Guid ContentId = hc.TemplateOverides.First(m => (m.PageId == hp.PageId && m.TemplateContentId == PluginId && m.IsDroppedYN == "N")).OverideContentId;
            page.ContentText = hc.HtmlContents.First(m => m.ContentId == ContentId).ContentText;
            page.HtmlPageId = id;
            page.HtmlContentId = ContentId;

            List<Permission> plist = hc.Permissions.ToList();
            ViewBag.PermissionList = plist;
            return View(page);
        }

        //
        // POST: /HtmlContent/Edit

        [HttpPost]
        public ActionResult Edit(Models.Page p)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    HtmlContentEntities hc = new HtmlContentEntities();
                    Guid PluginId = new Guid(((System.Runtime.InteropServices.GuidAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false)[0])).Value);
                    Models.Page pPage = hc.Pages.First(m => m.PageId == p.PageId);
                    string pOnMainMenuYN = pPage.OnMainMenuYN;
                    string pPageName = pPage.PageName;

                    Models.HtmlContent c = hc.HtmlContents.First(m => m.ContentId == p.HtmlContentId);
                    c.ContentText = p.ContentText;
                    c.ModifiedDT = DateTime.Now;
                    c.ModifiedById = (Guid)Session["UserId"];
                    c.ModifiedByName = Session["UserFullName"].ToString();

                    Models.HtmlPage hp = hc.HtmlPages.First(m => m.HtmlPageId == p.HtmlPageId);
                    hp.PageName = p.Title;                    
                    hp.ModifiedDT = DateTime.Now;
                    hp.ModifiedById = (Guid)Session["UserId"];
                    hp.ModifiedByName = Session["UserFullName"].ToString();

                    pPage.PageName = p.PageName;
                    pPage.Title = p.Title;
                    pPage.ShowTitleYN = p.ShowTitleYN;
                    pPage.OnMainMenuYN = p.OnMainMenuYN;
                    pPage.PermissionId = p.PermissionId;
                    pPage.MetaKeywords = p.MetaKeywords;
                    pPage.MetaDescription = p.MetaDescription;
                    pPage.ModifiedDT = DateTime.Now;
                    pPage.ModifiedById = (Guid)Session["UserId"];
                    pPage.ModifiedByName = Session["UserFullName"].ToString();

                    hc.SaveChanges();

                    // Update the MainMenu entry, create one if required.
                    if (pOnMainMenuYN != p.OnMainMenuYN)
                    {
                        Core.Models.CoreEntities core = new Core.Models.CoreEntities();
                        string pMenuUrl = pPageName.Substring(1);
                        List<Core.Models.MainMenu> mlist = core.MainMenus.Where(m => (m.URL == pMenuUrl && m.IsDroppedYN == "N")).ToList();
                        if (p.OnMainMenuYN == "Y" && mlist.Count <= 0)
                        {
                            Core.Models.MainMenu menu = new Core.Models.MainMenu();
                            menu.MenuItemId = Guid.NewGuid();
                            menu.DomainId = (Guid)Session["DomainId"];
                            menu.Name = p.Title;
                            menu.URL = p.PageName.Substring(1);
                            menu.UserPermission = hc.Permissions.First(m => m.PermissionId == p.PermissionId).Name;
                            menu.Sequence = 1;
                            menu.IsDroppedYN = "N";
                            menu.CreateDT = DateTime.Now;
                            menu.CreatedById = (Guid)Session["UserId"];
                            menu.CreatedByName = Session["UserFullName"].ToString();
                            menu.ModifiedDT = DateTime.Now;
                            menu.ModifiedById = (Guid)Session["UserId"];
                            menu.ModifiedByName = Session["UserFullName"].ToString();
                            core.AddToMainMenus(menu);
                            core.SaveChanges();
                        }
                        else
                        {
                            Core.Models.MainMenu menu = mlist[0];
                            menu.IsDroppedYN = p.OnMainMenuYN == "Y" ? "N" : "Y";
                            menu.ModifiedDT = DateTime.Now;
                            menu.ModifiedById = (Guid)Session["UserId"];
                            menu.ModifiedByName = Session["UserFullName"].ToString();
                            core.SaveChanges();
                        }
                    }

                    // Index the content
                    Core.Search.SearchDoc doc = new Core.Search.SearchDoc();
                    doc.DocId = p.PageId;
                    doc.BusinessUnitId = Guid.Empty; // Search convention is Guid.EWmpty if Domain wide access
                    doc.PluginId = PluginId;
                    doc.URL = p.PageName.Substring(1);
                    doc.Title = p.Title;
                    doc.Keywords = p.MetaKeywords;
                    doc.Description = p.MetaDescription;
                    doc.Content = Core.Tools.GetTextFromHtml(p.ContentText);
                    Core.Search.SearchEngine se = new Core.Search.SearchEngine((Guid)Session["DomainId"]);
                    se.IndexContent(doc);

                    try
                    {
                        if (!String.IsNullOrEmpty(TempData["ReturnURL"].ToString())) return Redirect(TempData["ReturnURL"].ToString());
                    }
                    catch
                    {
                    }
                    return RedirectToAction("Index");
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", Tools.ExceptionMessage(e));
                }
            }
            return View(p);
        }

        //
        // GET: /HtmlContent/Details

        public ActionResult Details(Guid id)
        {
            HtmlContentEntities hc = new HtmlContentEntities();
            Guid PluginId = new Guid(((System.Runtime.InteropServices.GuidAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false)[0])).Value);

            Models.HtmlPage hp = hc.HtmlPages.First(m => m.HtmlPageId == id);
            Models.Page page = hc.Pages.First(m => m.PageId == hp.PageId);
            Guid ContentId = hc.TemplateOverides.First(m => (m.PageId == hp.PageId && m.TemplateContentId == PluginId && m.IsDroppedYN == "N")).OverideContentId;
            page.ContentText = hc.HtmlContents.First(m => m.ContentId == ContentId).ContentText;
            page.HtmlPageId = id;
            page.HtmlContentId = ContentId;

            ViewBag.PermissionName = hc.Permissions.First(m => m.PermissionId == page.PermissionId).Name;
            return View(page);
        }

        //
        // GET: /HtmlContent/Delete

        public ActionResult Delete(Guid id)
        {
            HtmlContentEntities hc = new HtmlContentEntities();
            Guid PluginId = new Guid(((System.Runtime.InteropServices.GuidAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false)[0])).Value);

            Models.HtmlPage hp = hc.HtmlPages.First(m => m.HtmlPageId == id);
            Models.Page page = hc.Pages.First(m => m.PageId == hp.PageId);
            Guid ContentId = hc.TemplateOverides.First(m => (m.PageId == hp.PageId && m.TemplateContentId == PluginId && m.IsDroppedYN == "N")).OverideContentId;
            page.ContentText = hc.HtmlContents.First(m => m.ContentId == ContentId).ContentText;
            page.HtmlPageId = id;
            page.HtmlContentId = ContentId;

            ViewBag.PermissionName = hc.Permissions.First(m => m.PermissionId == page.PermissionId).Name;
            return View(page);
        }

        //
        // POST: /HtmlContent/Delete

        [HttpPost]
        public ActionResult Delete(Guid HtmlPageId, Guid HtmlContentId)
        {
            HtmlContentEntities hc = new HtmlContentEntities();
            Core.Models.CoreEntities core = new Core.Models.CoreEntities();
            Guid PluginId = new Guid(((System.Runtime.InteropServices.GuidAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false)[0])).Value);
            Models.HtmlPage hp;

            try
            {
                hp = hc.HtmlPages.First(m => m.HtmlPageId == HtmlPageId);
                hp.IsDroppedYN = "Y";
                hp.ModifiedDT = DateTime.Now;
                hp.ModifiedById = (Guid)Session["UserId"];
                hp.ModifiedByName = Session["UserFullName"].ToString();

                Models.Page p = hc.Pages.First(m => (m.PageId == hp.PageId && m.IsDroppedYN == "N"));
                string pOnMainMenuYN = p.OnMainMenuYN;
                string pPageName = p.PageName;
                p.IsDroppedYN = "Y";
                p.ModifiedDT = DateTime.Now;
                p.ModifiedById = (Guid)Session["UserId"];
                p.ModifiedByName = Session["UserFullName"].ToString();

                Models.HtmlContent c = hc.HtmlContents.First(m => m.ContentId == HtmlContentId);
                c.IsDroppedYN = "Y";
                c.ModifiedDT = DateTime.Now;
                c.ModifiedById = (Guid)Session["UserId"];
                c.ModifiedByName = Session["UserFullName"].ToString();

                Models.TemplateOveride to = hc.TemplateOverides.First(m => (m.PageId == p.PageId && m.TemplateContentId == p.Container.ContentId && m.OverideContentId == HtmlContentId));
                to.IsDroppedYN = "Y";
                to.ModifiedDT = DateTime.Now;

                hc.SaveChanges();

                // Delete Mainmenu entry if Specified
                if (p.OnMainMenuYN == "Y")
                {
                    string pMenuUrl = pPageName.Substring(1);
                    List<Core.Models.MainMenu> mlist = core.MainMenus.Where(m => (m.URL == pMenuUrl && m.IsDroppedYN == "N")).ToList();
                    if (mlist.Count > 0)
                    {
                        Core.Models.MainMenu menu = mlist[0];
                        menu.IsDroppedYN = "Y";
                        menu.ModifiedDT = DateTime.Now;
                        menu.ModifiedById = (Guid)Session["UserId"];
                        menu.ModifiedByName = Session["UserFullName"].ToString();
                        core.SaveChanges();
                    }
                }

                // Remove the content from the Index
                Core.Search.SearchEngine se = new Core.Search.SearchEngine((Guid)Session["DomainId"]);
                se.RemoveContent(p.PageId);

                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", Tools.ExceptionMessage(e));
            }

            hp = hc.HtmlPages.First(m => m.HtmlPageId == HtmlPageId);
            Models.Page page = hc.Pages.First(m => m.PageId == hp.PageId);
            Guid ContentId = hc.TemplateOverides.First(m => (m.PageId == hp.PageId && m.TemplateContentId == PluginId && m.IsDroppedYN == "N")).OverideContentId;
            page.ContentText = hc.HtmlContents.First(m => m.ContentId == ContentId).ContentText;
            page.HtmlPageId = HtmlPageId;
            page.HtmlContentId = ContentId;

            ViewBag.PermissionName = hc.Permissions.First(m => m.PermissionId == page.PermissionId).Name;
            return View(page);
        }

        // Return Count of documents if IsReturnDocCountRequest is true.
        // Return Documents processed in this partition even if they are 0, if the partition cannot be processed return -1
        [HttpPost]
        public JsonResult Reindex(int partition, bool IsReturnDocCountRequest)
        {
            int retval = 0;
            HtmlContentEntities hc = new HtmlContentEntities();
            Guid DomainId = (Guid)Session["DomainId"];
            if (IsReturnDocCountRequest)
            {
                List<BusinessUnit> BUList = hc.BusinessUnits.Where(m => (m.DomainId == DomainId && m.IsDroppedYN == "N")).ToList();
                foreach (BusinessUnit bu in BUList)
                {
                    retval += hc.HtmlPages.Where(m => (m.BusinessUnitId == bu.BusinessUnitId && m.IsDroppedYN == "N")).Count();
                }
            }
            else
            {
                retval = -1;
                List<BusinessUnit> BUList = hc.BusinessUnits.Where(m => (m.DomainId == DomainId && m.IsDroppedYN == "N")).ToList();
                if (partition >= BUList.Count) return Json(retval);
                Guid PluginId = new Guid(((System.Runtime.InteropServices.GuidAttribute)(Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false)[0])).Value);
                BusinessUnit bu = BUList[partition];
                List<HtmlPage> pages = hc.HtmlPages.Where(m => (m.BusinessUnitId == bu.BusinessUnitId && m.IsDroppedYN == "N")).ToList();
                retval = pages.Count();
                foreach (HtmlPage page in pages)
                {
                    Models.Page p = hc.Pages.First(m => m.PageId == page.PageId);
                    // Index the content
                    Core.Search.SearchDoc doc = new Core.Search.SearchDoc();
                    doc.DocId = p.PageId;
                    doc.BusinessUnitId = Guid.Empty; // Search convention is Guid.Empty if Domain wide access
                    doc.PluginId = PluginId;
                    doc.URL = p.PageName.Substring(1);
                    doc.Title = p.Title;
                    doc.Keywords = p.MetaKeywords;
                    doc.Description = p.MetaDescription;
                    Guid ContentId = hc.TemplateOverides.First(m => (m.PageId == page.PageId && m.TemplateContentId == PluginId && m.IsDroppedYN == "N")).OverideContentId;
                    doc.Content = Core.Tools.GetTextFromHtml(hc.HtmlContents.First(m => m.ContentId == ContentId).ContentText);
                    Core.Search.SearchEngine se = new Core.Search.SearchEngine((Guid)Session["DomainId"]);
                    se.IndexContent(doc);
                }
            }
            return Json(retval);
        }

    }    
}
